package com.example.bookstore.api

import com.example.bookstore.dao.BookCategoryDao
import com.example.bookstore.entity.BookCategoryEntity
import com.example.bookstore.entity.BookEntity
import com.example.bookstore.service.BookCategoryService
import com.example.bookstore.validate.PageNSizeValidate
import com.fasterxml.jackson.annotation.JsonView
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.constraints.NotBlank

/**
 * Created by IntelliJ IDEA.
 * Book: lvshe
 * Date: 2019/8/10
 * Time: 15:14
 */
@RestController
@RequestMapping("/api/category")
@Api(tags = ["书籍分类"], basePath = "/api/category")
class BookCategoryController : BaseController<BookCategoryService, BookCategoryDao, BookCategoryEntity>() {

    @ApiOperation("获取书籍分类列表", notes = "获取书籍分类列表，根据order从小到大排序返回")
    @GetMapping("/list")
    @JsonView(BookCategoryEntity.BookCategoryViewWithBookWithCategory::class)
    override fun getAllPageable(@Validated pageNSizeValidate: PageNSizeValidate): MutableList<BookCategoryEntity> {
        return super.getAllPageable(pageNSizeValidate).sortedBy { it.order }.toMutableList()
    }

    @ApiOperation("获取指定IDs书籍分类列表", notes = "获取指定IDs书籍分类列表，根据order从小到大排序返回")
    @ApiImplicitParams(ApiImplicitParam("书籍分类IDs，多个ID用英文逗号,分开，可传入单个书籍分类ID", name = "ids", required = true))
    @GetMapping("/books/{ids}")
    fun getBooksByIds(@NotBlank(message = "ids 不能为空") @PathVariable ids: String?): List<BookEntity> {
        return service.getBooksByIds(ids!!)
    }

}